import {
  fetchUserCenter
} from '../../services/usercenter/fetchUsercenter';
import {
  staticCount
} from '~/services/api/login';
import {
  exit
} from '~/services/api/login';
import {
  getMenberDetail
} from '../../services/api/menber';
import Toast from 'tdesign-miniprogram/toast/index';
const orderTagInfos = [{
    title: '待付款',
    iconName: 'wallet',
    orderNum: 0,
    tabType: 5,
    status: 1,
  },
  {
    title: '待审核',
    iconName: 'check',
    orderNum: 0,
    tabType: 10,
    status: 2,
  },
  {
    title: '待备货',
    iconName: 'deliver',
    orderNum: 0,
    tabType: 40,
    status: 3,
  },
  {
    title: '审核拒绝',
    iconName: 'wallet',
    orderNum: 0,
    tabType: 60,
    status: 4,
  },
  {
    title: '已发货',
    iconName: 'package',
    orderNum: 0,
    tabType: 0,
    status: 5,
  },
  {
    title: '已收货',
    iconName: 'package',
    orderNum: 0,
    tabType: 0,
    status: 6,
  },
  {
    title: '已取消',
    iconName: 'exchang',
    orderNum: 0,
    tabType: 0,
    status: 7,
  },
];
const menuData = [
  [{
      title: '待确认收货订单数',
      tit: '6',
      url: '',
      type: 'unConfirmNews',
    },
    {
      title: '未阅读消息',
      tit: '6',
      url: '',
      type: 'unreadNews',
    },
  ],
  [{
      title: '代理商',
      tit: '',
      url: '',
      type: 'agent',
    },
    {
      title: '产品管理',
      tit: '',
      url: '',
      type: 'product',
    },
    {
      title: '活动策划',
      tit: '',
      url: '',
      type: 'activePlan',
    },
    {
      title: '投诉建议',
      tit: '',
      url: '',
      type: 'advice',
    },
  ],
  [{
    title: '数据统计',
    tit: '',
    url: '',
    type: 'stat',
  }, ],
  [{
      title: '学习园地',
      tit: '',
      url: '',
      type: 'resourcePool',
    },

    {
      title: '退出登录',
      tit: '',
      url: '',
      type: 'loginout',
      icon: '',
    },
  ],
];
const getDefaultData = () => ({
  showMakePhone: false,
  menuData,
  orderTagInfos,
  customerServiceInfo: {},
  currAuthStep: 1,
  showKefu: true,
  versionNo: '',
});

Page({
  data: {
    countInfo: {},
    userInfo: {
      avatarUrl: '',
      nickName: '',
      phoneNumber: '',
    },
    ...getDefaultData(),
  },

  onLoad() {},

  async onShow() {
    this.getTabBar().init();
    await this.initData();
    this.init();
  },
  onPullDownRefresh() {
    this.init();
  },
  async initData() {
    const {
      data,
      code
    } = await staticCount();
    console.log(data, code);
    console.log(Object.keys(data));
    if (code == 200) {
      // this.countInfo =  data;
      this.setData({
        countInfo: data,
      });
    }
  },

  init() {
    this.fetUseriInfoHandle();
    const role = wx.getStorageSync('role');
    this.getDetailData(role.userId);
  },
  async getDetailData(kid) {
    const that = this;
    const res = await getMenberDetail({
      kid,
    });
    if (res.code == '200') {
      const result = res.data;
      console.log(result);
      that.setData({
        userInfo: result,
      });
    }
  },

  fetUseriInfoHandle() {
    fetchUserCenter().then(({
      countsData,
      orderTagInfos: orderInfo,
      customerServiceInfo
    }) => {
      menuData[0][0].tit = this.data.countInfo.todoOrderCount;
      menuData[0][1].tit = this.data.countInfo.todoNotifyMsgCount;
      console.log(this.data.countInfo, ' this.data.countInfo');

      this.setData({
        // userInfo,
        menuData,
        orderTagInfos,
        customerServiceInfo,
        currAuthStep: 2,
      });
      wx.stopPullDownRefresh();
    });
  },

  async onClickCell({
    currentTarget
  }) {
    const {
      type
    } = currentTarget.dataset;
    console.log(type);

    switch (type) {
      case 'agent':
        wx.navigateTo({
          url: '/subpages/pages/allTeams/allTeams',
        });
        break;
      case 'product':
        wx.navigateTo({
          url: '/subpages/pages/goods/category/index',
        });
        break;
      case 'activePlan':
        wx.navigateTo({
          url: '/subpages/pages/activePlan/list/list',
        });
        break;
      case 'unConfirmNews':
        wx.navigateTo({
          url: '/subpages/pages/storeOrder/index',
        });
        break;
      case 'unreadNews':
        wx.switchTab({
          url: '/pages/news/news',
        });
        break;
      case 'stat':
        wx.navigateTo({
          url: '/subpages/pages/countList/countList',
        });
        break;
      case 'advice':
        // 代理商页面跳转
        wx.navigateTo({
          url: '/subpages/pages/suggestList/suggestList?isAgentStatus=true',
        });

        break;
      case 'loginout': {
        const {
          data
        } = await exit();
        let reminberP = wx.getStorageSync('reminberP');
        let username = wx.getStorageSync('username');
        let password = wx.getStorageSync('password');
        wx.clearStorageSync();
        if (reminberP) {
          wx.setStorageSync('username', username);
          wx.setStorageSync('password', password)
          wx.setStorageSync('reminberP', true)
        }
        wx.reLaunch({
          url: '/pages/login/login',
        });
        break;
      }
      case 'order':
        wx.navigateTo({
          url: '/subpages/pages/saleOrder/saleOrder',
        });
        break;
      case 'resourcePool': {
        wx.navigateTo({
          url: '/subpages/pages/resourcePool/resourcePool',
        });
      }
      // default: {
      //   Toast({
      //     context: this,
      //     selector: '#t-toast',
      //     message: '未知跳转',
      //     icon: '',
      //     duration: 1000,
      //   });
      //   break;
      // }
    }
  },
  jumpNav(e) {
    const {
      status
    } = e.detail;
    wx.navigateTo({
      url: `/subpages/pages/order-list/index?status=${status}`,
    });
  },

  jumpAllOrder() {
    wx.navigateTo({
      url: '/subpages/pages/order-list/index',
    });
  },

  openMakePhone() {
    this.setData({
      showMakePhone: true,
    });
  },

  closeMakePhone() {
    this.setData({
      showMakePhone: false,
    });
  },

  call() {
    wx.makePhoneCall({
      phoneNumber: this.data.customerServiceInfo.servicePhone,
    });
  },

  gotoUserEditPage() {
    // 修改个人中心资料
    const role = wx.getStorageSync('role');
    wx.navigateTo({
      url: `/subpages/pages/addNewAccount/addNewAccount?editSelf=true&kid=${role.userId}`,
    });
  },
});